import axios from "@/utils/http";

//数据
const state = {
    tableData:[],
    tableTotal:1,
    tableDataDia:[],
    tableTotalDia:1
  };
  
  const getters = {
    
    getMessage(state:any){
        //获取修饰或筛选后的数据
        let arr  = state.tableData;
        // console.log(arr);
        
        // arr.forEach((element:any) => {
        //   let date = new Date(element.createDate)
        //   element.createDate = date.toLocaleDateString() +" " +date.toLocaleTimeString()
        // });
        return arr
    },
    getMessageDia(state:any){
      //获取修饰或筛选后的数据
      let arr  = state.tableDataDia;
      // console.log(arr);
      
      arr.forEach((element:any) => {
        let date = new Date(element.createDate)
        element.createDate = date.toLocaleDateString() +" " +date.toLocaleTimeString()
      });
      return arr
    
  },  

    getTableData(state:any){
        return state.tableData;
    }
  };
  

  const actions = {};
  
  const mutations = {

    setMessageData(state:any,searchObj:any){
      
      
      axios({
        url:"/medicine/message/findDrugList",
        method: "get",
        params: {
          drugName:searchObj.drugName,
          drugSort:searchObj.drugSort,
          currentPage:searchObj.currentPage,
          pageSize:searchObj.pageSize,
          status:searchObj.status,
        },
      }).then((res)=>{
        console.log(res);
        if(searchObj.status){
          if(res.data.data=="查询失败"){
            state.tableData=[];
            state.tableTotal=0
          }else{
            state.tableData=res.data.data
            state.tableTotal=res.data.count
          }
        }else{
          if(res.data.data=="查询失败"){
            state.tableDataDia=[];
            state.tableTotalDia=0
          }else{
            state.tableDataDia=res.data.data
            state.tableTotalDia=res.data.count
          }
          
        
        }
       })
      
    },
    searchData(state:any,searchObj:any){
      console.log(searchObj);
      
      axios({
        url:"/medicine/message/findDrugList",
        method: "get",
        params: {
          drugName:searchObj.drugName,
          drugSort:searchObj.drugSort,
          currentPage:searchObj.currentPage,
          pageSize:searchObj.pageSize,
          status:searchObj.status,
        },
      }).then((res)=>{
        console.log(res);
        if(searchObj.status){
          state.tableData=res.data.data
          state.tableTotal=res.data.count
        }else{
          state.tableDataDia=res.data.data
        state.tableTotalDia=res.data.count
        }
       })
      
    },



    filterData(state:any,str:string){
      console.log("mutations"+str);
      
        let filterarr:any=[];
        state.tableData.forEach((item:any) => {
            if(item.name==str||str==""){
              filterarr.push(item)
            }
        });
        state.tableData=filterarr;
        state.total=state.tableData.length
    },


   addData(state:any,todo:any){
    console.log(todo);
    let dataArr = JSON.parse(JSON.stringify(todo.value))
    dataArr.forEach((item:any) => {
      axios({
        url:"/medicine/message/addDrug",
        method:"post",
        params:{
          id:item.id,
          status:1
        }
      }).then((res)=>{
        console.log(res);
        // this.commit('setMessageData')
      })
    })
   
   },
   deleteData(state:any,id:any){
    // console.log(id);
      axios({
        url:"/medicine/message/deleteDrug",
        method:"post",
        params:{
          id:id,
          status:0
        }
      }).then((res)=>{
        console.log(res);
        
      })
   
   },

  };
  
  export default {
    state,
    mutations,
    actions,
    getters,
  };
  